package cn.shengchao.examstar.knowledge.acl.adapters.repositories;

import cn.shengchao.examstar.knowledge.acl.ports.repositories.IKnowledgePointRepository;
import cn.shengchao.examstar.knowledge.domain.entity.KnowledgePoint;
import cn.shengchao.examstar.knowledge.domain.entity.KnowledgeTag;
import cn.shengchao.examstar.knowledge.infrastructure.dao.TKnowledgePointTagMapper;
import cn.shengchao.examstar.knowledge.infrastructure.dao.TKnowledgePointTreeMapper;
import cn.shengchao.examstar.knowledge.infrastructure.dao.po.TKnowledgePointTag;
import cn.shengchao.examstar.knowledge.infrastructure.dao.po.TKnowledgePointTree;
import cn.shengchao.examstar.rpc.knowledge.req.KnowledgeReq;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class KnowledgePointRepository implements IKnowledgePointRepository {

    @Autowired
    private TKnowledgePointTagMapper tKnowledgePointTagMapper;

    @Autowired
    private TKnowledgePointTreeMapper tKnowledgePointMapper;

    @Override
    public List<KnowledgeTag> getKnowledgePointTagList(Long id) {
        List<TKnowledgePointTag> knowledgePointTags = tKnowledgePointTagMapper.selectList(Wrappers.lambdaQuery(TKnowledgePointTag.class).eq(TKnowledgePointTag::getKnowledgePointTreeId, id));
        return knowledgePointTags.stream().map(
                knowledgePointTag -> new KnowledgeTag(knowledgePointTag.getId(), knowledgePointTag.getTag(), knowledgePointTag.getTagId(), knowledgePointTag.getColor())).toList();
    }


    @Override
    public void bindKnowledgePointTag(Long id, List<KnowledgeTag> tagIds) {
        tKnowledgePointTagMapper.delete(Wrappers.lambdaQuery(TKnowledgePointTag.class).eq(TKnowledgePointTag::getKnowledgePointTreeId, id));
        for (KnowledgeTag tagId : tagIds) {
            TKnowledgePointTag tKnowledgePointTag = new TKnowledgePointTag();
            tKnowledgePointTag.setKnowledgePointTreeId(id);
            tKnowledgePointTag.setTagId(tagId.getTagId());
            tKnowledgePointTag.setTag(tagId.getTag());
            tKnowledgePointTag.setColor(tagId.getColor());
            tKnowledgePointTagMapper.insert(tKnowledgePointTag);
        }
    }

    @Override
    public List<KnowledgePoint> getKnowledgePointByQuestionLeafList(KnowledgeReq knowledgeReq) {
        return tKnowledgePointMapper.getKnowledgePointByQuestionLeafList(knowledgeReq);
    }

    @Override
    public List<KnowledgePoint> getKnowledgePointByLeafList(KnowledgeReq knowledgeReq) {
        return tKnowledgePointMapper.getKnowledgePointByLeafList(knowledgeReq);
    }


    @Override
    public List<KnowledgePoint> getKnowledgePointByPath(String knowledgePointPath) {
        List<TKnowledgePointTree> tKnowledgePointTrees = tKnowledgePointMapper.selectList(Wrappers.lambdaQuery(TKnowledgePointTree.class)
                .likeRight(TKnowledgePointTree::getPath, knowledgePointPath)
                .eq(TKnowledgePointTree::getStatus, 1)
        );
        if (tKnowledgePointTrees == null) {
            return List.of();
        }
        return tKnowledgePointTrees.stream().map(tKnowledgePointTree -> {
            KnowledgePoint knowledgePoint = new KnowledgePoint();
            knowledgePoint.setId(tKnowledgePointTree.getId());
            knowledgePoint.setName(tKnowledgePointTree.getName());
            knowledgePoint.setPath(tKnowledgePointTree.getPath());
            knowledgePoint.setParentId(tKnowledgePointTree.getParentId());
            knowledgePoint.setSort(tKnowledgePointTree.getSort());
            return knowledgePoint;
        }).toList();
    }

    @Override
    public KnowledgePoint getKnowledgePoint(Long id) {
        TKnowledgePointTree tKnowledgePointTree = tKnowledgePointMapper.selectById(id);
        KnowledgePoint knowledgePoint = new KnowledgePoint();
        knowledgePoint.setId(tKnowledgePointTree.getId());
        knowledgePoint.setName(tKnowledgePointTree.getName());
        knowledgePoint.setPath(tKnowledgePointTree.getPath());
        knowledgePoint.setParentId(tKnowledgePointTree.getParentId());
        knowledgePoint.setSort(tKnowledgePointTree.getSort());
        List<KnowledgeTag> knowledgeTags = getKnowledgePointTagList(id);
        knowledgePoint.setKnowledgeTags(knowledgeTags);
        return knowledgePoint;
    }
}
